﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;

namespace MWRCommunicationLibrary.ARS408
{
    public class Out_FilterState_Cfg : MWRData
    {
        public override int DefaultCanID
        {
            get
            {
                return 0x204;
            }
        }

        public override int Length
        {
            get
            {
                return 5;
            }
        }

        [ParamDecodor(2, 1)]
        public bool Active { get; set; }

        [ParamDecodor(3, 4)]
        public int Index { get; set; }

        [ParamDecodor(7, 1)]
        public int Type { get; set; }

        public double MinValue { get; set; }

        public double MaxValue { get; set; }

        private ParamDecodorAttribute[] MinParamList = new ParamDecodorAttribute[]
        {
            new ParamDecodorAttribute(16, 12),//Min_NofObj
            new ParamDecodorAttribute(16, 12, 0.1, 0),//Min_Distance unit:m
            new ParamDecodorAttribute(16, 12, 0.025, -50),//Azimuth unit:deg
            new ParamDecodorAttribute(16, 12, 0.0315, 0),//VrelOncome unitLm/s
            new ParamDecodorAttribute(16, 12, 0.0315, 0),//VrelDepart unit:m/s
            new ParamDecodorAttribute(16, 12, 0.025, -50),//RCS unit:dBm²
            new ParamDecodorAttribute(16, 12, 0.1, 0),//Lifetime unit:s
            new ParamDecodorAttribute(16, 12, 0.025, 0),//Size
            new ParamDecodorAttribute(16, 12),//ProbExists
            new ParamDecodorAttribute(16, 12, 0.2, -409.5),//Y
            new ParamDecodorAttribute(16, 13, 0.2, -500),//X
            new ParamDecodorAttribute(16, 12, 0.0315, 0),//VYRightLeft unit:m
            new ParamDecodorAttribute(16, 12, 0.0315, 0),//VXOncome
            new ParamDecodorAttribute(16, 12, 0.0315, 0),//VYLeftRight
            new ParamDecodorAttribute(16, 12, 0.0315, 0),//VXDepart
            new ParamDecodorAttribute(16, 12),//Object_Class
        };

        private ParamDecodorAttribute[] MaxParamList = new ParamDecodorAttribute[]
        {
            new ParamDecodorAttribute(32, 12),//Min_NofObj
            new ParamDecodorAttribute(32, 12, 0.1, 0),//Min_Distance unit:m
            new ParamDecodorAttribute(32, 12, 0.025, -50),//Azimuth unit:deg
            new ParamDecodorAttribute(32, 12, 0.0315, 0),//VrelOncome unitLm/s
            new ParamDecodorAttribute(32, 12, 0.0315, 0),//VrelDepart unit:m/s
            new ParamDecodorAttribute(32, 12, 0.025, -50),//RCS unit:dBm²
            new ParamDecodorAttribute(32, 12, 0.1, 0),//Lifetime unit:s
            new ParamDecodorAttribute(32, 12, 0.025, 0),//Size
            new ParamDecodorAttribute(32, 12),//ProbExists
            new ParamDecodorAttribute(32, 12, 0.2, -409.5),//Y
            new ParamDecodorAttribute(32, 13, 0.2, -500),//X
            new ParamDecodorAttribute(32, 12, 0.0315, 0),//VYRightLeft unit:m
            new ParamDecodorAttribute(32, 12, 0.0315, 0),//VXOncome
            new ParamDecodorAttribute(32, 12, 0.0315, 0),//VYLeftRight
            new ParamDecodorAttribute(32, 12, 0.0315, 0),//VXDepart
            new ParamDecodorAttribute(32, 12),//Object_Class
        };

        //public override byte[] GetSender(int radarID)
        //{
        //    Type type = this.GetType();
        //    var properties = type.GetProperties();

        //    //组装CANID
        //    byte[] msg_data = new byte[8];


        //    foreach (var p in properties)
        //    {
        //        ParamDecodorAttribute attr = (ParamDecodorAttribute)p.GetCustomAttribute(typeof(ParamDecodorAttribute));
        //        if (attr != null)
        //        {
        //            int value = 0;
        //            if (p.PropertyType == typeof(bool))
        //            {
        //                value = DecodeOperation.ConvertValueSender((bool)p.GetValue(this));
        //            }
        //            else if (p.PropertyType == typeof(int))
        //            {
        //                value = DecodeOperation.ConvertValueSender((int)p.GetValue(this), attr);
        //            }
        //            else if (p.PropertyType == typeof(double))
        //            {
        //                value = DecodeOperation.ConvertValueSender((double)p.GetValue(this), attr);
        //            }

        //            DecodeOperation.AssembleMsg(attr, value, ref msg_data);
        //        }
        //    }

        //    int v = DecodeOperation.ConvertValueSender(this.MinValue, this.MinParamList[this.Index]);
        //    DecodeOperation.AssembleMsg(this.MinParamList[this.Index], v, ref msg_data);
        //    v = DecodeOperation.ConvertValueSender(this.MaxValue, this.MaxParamList[this.Index]);
        //    DecodeOperation.AssembleMsg(this.MaxParamList[this.Index], v, ref msg_data);


        //    byte[] msg = new byte[13];
        //    int canID = this.DefaultCanID;
        //    canID = canID + radarID * 0x10;
        //    byte[] bCanID = BitConverter.GetBytes(canID);
        //    msg[0 + 3] = bCanID[1];
        //    msg[1 + 3] = bCanID[0];

        //    for (int i = 0; i < 8; i++)
        //    {
        //        msg[5 + i] = msg_data[i];
        //    }

        //    return msg;
        //}

        public override void FillData(byte[] data)
        {
            Type type = this.GetType();
            var properties = type.GetProperties();

            foreach (var p in properties)
            {
                ParamDecodorAttribute attr = (ParamDecodorAttribute)p.GetCustomAttribute(typeof(ParamDecodorAttribute));
                if (attr != null)
                {
                    var value = DecodeOperation.DecodeData(data, attr);
                    if (p.PropertyType == typeof(bool))
                    {
                        p.SetValue(this, value.Equals(1d));
                    }
                    else if (p.PropertyType == typeof(int))
                    {
                        p.SetValue(this, int.Parse(value.ToString()));
                    }
                    else if (p.PropertyType == typeof(double))
                    {
                        p.SetValue(this, double.Parse(value.ToString()));
                    }

                }
            }

            this.MinValue = double.Parse(DecodeOperation.DecodeData(data, this.MinParamList[this.Index]).ToString());
            this.MaxValue = double.Parse(DecodeOperation.DecodeData(data, this.MaxParamList[this.Index]).ToString());
        }
    }
}
